import { t } from 'i18next'
import { useEffect, useState } from 'react'
import { Message } from '@kedacom/react-next'
import { Alert } from '@/components/Alert'
import { useTvWallStore } from '../store'
import { useRequest } from 'ahooks'
import { useMccStore } from '../../../store'
import { Mt } from '@/api/manager/mt'
import { Poll } from '../interface'
import { fetchGetPollPlanDetail, fetchGetPollPlanList } from '@/api/manager/poll'

const pollIntervalOptions = [
	{ label: t('15(快)'), value: 15 },
	{ label: t('30(中)'), value: 30 },
	{ label: t('60(慢)'), value: 60 },
	{ label: t('自定义'), value: 0 },
]
export default function useService(data: Poll | null) {
	const meetingMoid = useMccStore((state) => state.mccInfo.meetingMoid)
	const { batchMembers } = useTvWallStore((state) => ({
		batchMembers: state.batchMembers,
	}))
	const [open, setOpen] = useState(false)
	const [planOptions, setPlanOptions] = useState<Array<{ label: string; value: string }>>([
		{ label: t('不使用预案'), value: '0' },
	])
	const [planValue, setPlanValue] = useState('0')
	const [pollIntervalOptionsVal, setPollIntervalOptionsVal] = useState(30)
	const [pollInterval, setPollInterval] = useState('5')
	const [pollTimes, setPollTimes] = useState('')
	const [unlimited, setUnlimited] = useState(true)
	const [pollMembers, setPollMembers] = useState<Mt[]>([])
	const { data: pollData } = useRequest(() => fetchGetPollPlanList(meetingMoid))
	useEffect(() => {
		if (pollData && pollData.success) {
			let arr = pollData.data ? pollData.data.map((item) => ({ label: item.name, value: item.id })) : []
			arr.unshift({ label: t('不使用预案'), value: '0' })
			setPlanOptions(arr)
		}
	}, [pollData])
	useEffect(() => {
		initData(data)
	}, [data])
	useEffect(() => {
		setPlanValue(planOptions.find((item) => item.label === data?.name)?.value ?? '0')
	}, [data, planOptions])
	const initData = (data: Poll | null) => {
		if (data) {
			setPlanValue(planOptions.find((item) => item.label === data.name)?.value ?? '0')
			setUnlimited(data.num === 0)
			setPollTimes(data.num === 0 ? '' : String(data.num))
			if (data.keep_time !== 15 && data.keep_time !== 30 && data.keep_time !== 60) {
				setPollIntervalOptionsVal(0)
				setPollInterval(String(data.keep_time))
			} else {
				setPollIntervalOptionsVal(data.keep_time)
				setPollInterval('5')
			}
			setPollMembers(data.members)
		} else {
			//默认值
			setPlanValue('0')
			setUnlimited(true)
			setPollInterval('30')
			setPollIntervalOptionsVal(30)
			setPollTimes('')
			setPollMembers(batchMembers as any)
		}
	}
	const changePlan = async (id: string) => {
		setPlanValue(id)
		if (id === '0') {
			initData(null)
			return
		}
		fetchGetPollPlanDetail(meetingMoid, id).then((res) => {
			if (res.success) {
				initData(new Poll(res))
				Message.info(t('预案载入成功'))
			} else {
				Alert(t('获取轮询预案失败'))
			}
		})
	}
	return {
		/** 轮询方弹窗是否打开 */
		open,
		/** 轮询方弹窗是否打开方法 */
		setOpen,
		/** 轮询间隔选项 */
		pollIntervalOptions,
		/** 轮询间隔选项值 */
		pollIntervalOptionsVal,
		/** 设置轮询间隔选项值 */
		setPollIntervalOptionsVal,
		/** 轮询间隔实际值 */
		pollInterval,
		/** 设置轮询间隔实际值 */
		setPollInterval,
		/** 不限次数 */
		unlimited,
		/** 设置不限次数 */
		setUnlimited,
		/** 轮询预案选项 */
		planOptions,
		/** 轮询预案选项值 */
		planValue,
		/** 载入预案 */
		changePlan,
		/** 轮询次数 */
		pollTimes,
		/** 设置轮询次数 */
		setPollTimes,
		/** 轮询成员 */
		pollMembers,
		/** 设置轮询成员 */
		setPollMembers,
	}
}
